import { defineStore } from 'pinia';
import { setStorage, getStorage } from '@/utils/storageData';
import configObj from "@/config"; 
let { strongToken } = configObj;
export const useUserStore = defineStore('main', {
	state: () => {
		return {
			token: getStorage(strongToken),
			userInfo: getStorage('userInfo'),
			socketUrl: getStorage('socketUrl'),
			list: {}, 
			menus:[],
			renderMenus:[
				{
					name: '客服',
					isMenu: 1,
					path: '/service',
					redirectName: '/service/index',
				},
				{
					name: '客服',
					isMenu: 1,
					path: '/transfer',
					redirectName: '/transfer/getToken',
				},
			],
			/* menus: getStorage(menu),
			renderMenus: getStorage(renderMenu), */
		};
	},
	getters: {},
	actions: {
		/**
		 * @description 登录
		 * @param {*} token
		 */
		async afterLogin(info) { 
			this.userInfo = this.userInfo || {};
			const { data: { loginServiceUserVo, token,socketUrl, shopLogo, shopName }} = info;
			this.userInfo.shopLogo = shopLogo;
			this.userInfo.shopName = shopName;
			this.userInfo = Object.assign(this.userInfo,loginServiceUserVo);
			this.token = token;
			this.socketUrl = socketUrl;
			setStorage(JSON.stringify(token), strongToken)
			setStorage(JSON.stringify(this.userInfo), 'userInfo')
			setStorage(JSON.stringify(loginServiceUserVo), 'serviceInfo')
			setStorage(JSON.stringify(socketUrl),'socketUrl');
		},
		/**
		* @description 设置token
		* @param {*} token
		*/
		async setToken(token) { 
			this.token = token;
			setStorage(JSON.stringify(token), strongToken)
		},
		/**
		* @description 设置店铺相关信息
		* @param {*} token
		*/
		async setUser(obj) {
			this.userInfo = this.userInfo || {};
			this.userInfo = Object.assign(this.userInfo,obj)
			setStorage(JSON.stringify(this.userInfo), 'userInfo')
		},
		/**
		* @description 设置客服websoket链接
		* @param {*} token
		*/
		async setSocketUrl(url) {
			this.socketUrl = url;
			setStorage(JSON.stringify(this.socketUrl), 'socketUrl')
		},
		/**
		 * @description 获取用户基本信息
		 * @param {*} token
		 */
		async getUserInfo(){
			const { data } = await AuthApi.getUserInfo({});
			this.userInfo.shop_name = data.shop_name;
			this.userInfo.version = data.version;
			this.userInfo.supplierUserId = data.user.supplierUserId;
			this.userInfo.appId = data.user.appId;
			setStorage(JSON.stringify(this.userInfo),'userInfo');
		},
		/**
		 * @description 退出登录
		 * @param {*} token
		 */
		afterLogout() {
			sessionStorage.clear();
			// deleteSessionStorage(null);
			// delCookie(strongToken,null);
			this.token = null;
			this.menus = null;
		},
	}
});
export default useUserStore;